import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/depreciation_entity.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';

import 'default_bottom_sheet.dart';

showDepreciationMethodSelectBottomSheet(
  BuildContext context,
  DepreciationEntity currentSelect,
) async {
  DepreciationEntity _currentSelect = currentSelect;
  await showDefaultBottomSheet(
    context,
    child: DepreciationMethodSelectBottomSheet<DepreciationEntity>(
      currentSelect: _currentSelect,
      onSelectValue: (data) {
        _currentSelect = data;
      },
      group: Config.settingData.depreciationType,
      buildDisplayName: (d) => d.name,
    ),
    title: S.current.depreciation_method,
    isScrollControlled: true,
  );
  return _currentSelect;
}

typedef BuildDisplayName<T> = String Function(T value);

String defaultBuildDisplayName<T>(T value) => value as String;

class DepreciationMethodSelectBottomSheet<T> extends StatefulWidget {
  final List<T> group;
  final T currentSelect;
  final ValueChanged<T> onSelectValue;
  final BuildDisplayName<T> buildDisplayName;
  final bool isList;

  const DepreciationMethodSelectBottomSheet(
      {Key key,
      @required this.group,
      @required this.onSelectValue,
      @required this.currentSelect,
      @required this.buildDisplayName,
      this.isList = false})
      : super(key: key);

  @override
  _DepreciationMethodSelectBottomSheetState<T> createState() =>
      _DepreciationMethodSelectBottomSheetState<T>();
}

class _DepreciationMethodSelectBottomSheetState<T>
    extends State<DepreciationMethodSelectBottomSheet<T>> {
  T _currentSelect;
  BuildDisplayName<T> _buildDisplayName;

  @override
  void initState() {
    super.initState();
    _currentSelect = widget.currentSelect;
    _buildDisplayName = widget.buildDisplayName;
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> children = [
      SizedBox(
        height: getDp(17),
        width: MediaQuery.of(context).size.width,
      ),
      Padding(
        padding: EdgeInsets.symmetric(
          horizontal: getDp(10),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: widget.group.map((e) {
            if ((e as DepreciationEntity).key == 'manual' &&
                _currentSelect == e) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  PressedOpacityButton(
                    onPressed: () => onChangeSelect(e),
                    padding: EdgeInsets.symmetric(
                      vertical: getDp(10),
                      horizontal: getDp(34),
                    ),
                    child: NormalRadio(
                      title: _buildDisplayName?.call(e),
                      isSelect: _currentSelect == e,
                      isFix: true,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(left: getDp(68)),
                    child: Text(
                      S.current
                          .you_will_need_to_record_depreciation_manually_via_journal_entries,
                      style: TextStyle(
                        color: aceBlackColor,
                        fontSize: getSp(18),
                        fontWeight: FontWeight.w400,
                        height: 22 / 18,
                      ),
                    ),
                  ),
                ],
              );
            } else {
              return PressedOpacityButton(
                onPressed: () => onChangeSelect(e),
                padding: EdgeInsets.symmetric(
                  vertical: getDp(10),
                  horizontal: getDp(34),
                ),
                child: NormalRadio(
                  title: _buildDisplayName?.call(e),
                  isSelect: _currentSelect == e,
                  isFix: true,
                ),
              );
            }
          }).toList(),
        ),
      ),
    ];
    if (widget.isList == true) {
      double height = MediaQuery.of(context).size.height / 2;
      return SizedBox(
        height: height,
        child: ListView(
          children: children,
        ),
      );
    } else {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: children,
      );
    }
  }

  void onChangeSelect(e) {
    if (_currentSelect == e) return;
    setState(() {
      _currentSelect = e;
    });
    widget.onSelectValue?.call(_currentSelect);
    Navigator.of(context).pop();
  }
}
